import { Tsrv, ifs, ts } from '@/services';
import { N_cGanttChart } from './types';

class T_cmd {
  // 主组件实例
  private self: N_cGanttChart.cGanttChart;

  // 主组件created里初始化
  public init(self: N_cGanttChart.cGanttChart) {
    this.self = self;
  }

  /**
   * 处理数据重叠情况
   */
  private processOverlaps(
    data: N_cGanttChart.GanttData
  ): N_cGanttChart.GanttData {
    // 按开始时间排序
    const sortedItems = [...data.items].sort(
      (a, b) => a.startTime.getTime() - b.startTime.getTime()
    );

    return {
      ...data,
      items: sortedItems
    };
  }

  public doLoadData(tableName: string, filter = {}) {
    const gpd = new ts.T_getPartDataArg();
    gpd.setFilter(filter);
    gpd.setFunName(tableName);
    gpd.setTableName(tableName);
    gpd.setDbType(Tsrv.globalVar.dbType.MYSQL);
    gpd.setLang(this.self.$i18n.locale);
    gpd.setPage(1, 99999);
    gpd.setRule({} as ifs.I_rule);

    return Tsrv.getData(gpd);
  }

  public getTaskData() {
    const Tfilter = new ts.T_filter();
    const gteCondition = Tfilter.parseGTE(
      'create_time',
      this.getDayTimeRange(this.self.selectedDate).startTime
    );
    const lteCondition = Tfilter.parseLTE(
      'create_time',
      this.getDayTimeRange(this.self.selectedDate).endTime
    );
    const eqCondition = Tfilter.parseEQ('is_automated_produc', 1);
    const eqCondition1 = Tfilter.parseEQ('f_status', 6);
    const filter = Tfilter.parseAND([
      gteCondition,
      lteCondition,
      eqCondition,
      eqCondition1
    ]);
    return this.doLoadData('produc_line_detail_mgmt', filter).then(
      ([b, res, rows, err]) => {
        if (!b) {
          this.self.$msg(err);
        } else {
          this.self.taskData = rows;
        }
      }
    );
  }

  // 格式化日期
  public formatDate(date: Date): string {
    return `${date.getFullYear()}-${(date.getMonth() + 1)
      .toString()
      .padStart(2, '0')}-${date
      .getDate()
      .toString()
      .padStart(2, '0')}`;
  }

  /**
   * 获取日期的起始和结束时间
   * @param dateStr YYYY-MM-DD格式的日期字符串
   * @returns 包含startTime和endTime的对象
   */
  public getDayTimeRange(
    dateStr: string
  ): { startTime: string; endTime: string } {
    const date = new Date(dateStr);
    if (isNaN(date.getTime())) {
      throw new Error('Invalid date string');
    }

    // 当天起始时间 (00:00:00)
    const startTime = new Date(date);
    startTime.setHours(0, 0, 0, 0);

    // 当天结束时间 (23:59:59)
    const endTime = new Date(date);
    endTime.setHours(23, 59, 59, 999);

    // 格式化为YYYY-MM-DD HH:mm:ss
    const formatTime = (d: Date) => {
      return (
        `${d.getFullYear()}-${(d.getMonth() + 1)
          .toString()
          .padStart(2, '0')}-${d
          .getDate()
          .toString()
          .padStart(2, '0')} ` +
        `${d
          .getHours()
          .toString()
          .padStart(2, '0')}:${d
          .getMinutes()
          .toString()
          .padStart(2, '0')}:${d
          .getSeconds()
          .toString()
          .padStart(2, '0')}`
      );
    };

    return {
      startTime: formatTime(startTime),
      endTime: formatTime(endTime)
    };
  }
}

interface I_cmd extends T_cmd {}

export { I_cmd, T_cmd };
